import Strings from '@/i18n';

const hex2Dec = (hex: string) => {
  return parseInt(hex, 16);
};

const number2hex = (number, length = 2) => {
  return number.toString(16).padStart(length, '0');
};

export const decodeClock = (str: string, code?: string) => {
  const defaulTime = {
    clock_1: 420,
    clock_2: 14 * 60,
    clock_3: 22 * 60,
    clock_4: 480,
    clock_5: 600,
    clock_6: 12 * 60,
    clock_7: 18 * 60,
  };
  if (!str?.length || str?.length < 24 || str === '000000000000000000000000')  
    return {
      status: false,

      startTime: defaulTime[code || 'clock_1'],

      repeat: [1, 1, 1, 1, 1, 1, 1],

      weekRepeatPower: true,

      hue: 33,

      saturation: 1000,

      value: 1000,

      voice: 60,

      music: 1,

      code: code,

      unSetted: true,
    };

  try {
    let i = 0;

    const power = str.slice(i, i + 2);
    i += 2;

    const startTime = str.slice(i, i + 4);
    i += 4;

    const repeat = str.slice(i, i + 2);

    const repeat1 = parseInt(repeat.slice(0, 1), 16)
      .toString(2)
      .padStart(4, '0')
      .split('')
      .map(item => +item);

    const repeat2 = parseInt(repeat.slice(1), 16)
      .toString(2)
      .padStart(4, '0')
      .split('')
      .map(item => +item);

    const orginWeek = [...repeat1, ...repeat2].reverse().slice(0, 7);

    i += 2;

    const weekRepeatPower = str.slice(i, i + 2);
    i += 2;

    const musicOrder = str.slice(i, i + 2);
    i += 2;

    const hue = str.slice(i, i + 4);
    i += 4;

    const saturation = str.slice(i, i + 4);
    i += 4;

    const value = str.slice(i, i + 4);
    i += 4;

    const musicVolume = str.slice(i, i + 2);
    i += 2;

    return {
      status: power === '01',

      startTime: hex2Dec(startTime),

      repeat: orginWeek,

      weekRepeatPower: weekRepeatPower === '01',

      hue: hex2Dec(hue),

      saturation: hex2Dec(saturation),

      value: hex2Dec(value),

      music: hex2Dec(musicOrder),

      voice: hex2Dec(musicVolume),

      code: code,
    };
  } catch (error) {
    return {
      setted: false,
    };
  }
};

export const encodeClock = params => {
  const {
    status,

    startTime,

    repeat,

    weekRepeatPower,

    hue,

    saturation,

    value,

    voice,

    music,

    code,
  } = params;

  const powerStr = status ? '01' : '00';

  const startTimeStr = number2hex(startTime, 4);

  const newWeeks = [...repeat, 0].reverse();

  const week1Str = parseInt(newWeeks.slice(0, 4).join(''), 2).toString(16);

  const week2Str = parseInt(newWeeks.slice(4).join(''), 2).toString(16);

  const weekStr = `${week1Str}${week2Str}`;

  const weekRepeatPowerStr = weekRepeatPower ? '01' : '00';

  const hueStr = number2hex(hue, 4);

  const saturationStr = number2hex(saturation, 4);

  const valueStr = number2hex(value, 4);

  const musicOrderStr = number2hex(music);

  const voiceStr = number2hex(voice);

  return `${powerStr}${startTimeStr}${weekStr}${weekRepeatPowerStr}${musicOrderStr}${hueStr}${saturationStr}${valueStr}${voiceStr}`;
};

export const repeatArrtoStr = (originSource: number[]) => {
  if (!originSource) return '';
  const source = originSource.map(i => +i);

  const days: string[] = [];
  let repeat = '';
  let workDay = true;
  let weekend = true;
  source.forEach((item, index) => {
    if (index > 0 && index < 6) {
      if (item === 0) {
        workDay = false;
      }
    }
    if (index === 0 || index === 6) {
      if (item === 0) {
        weekend = false;
      }
    }
    if (item === 1) {
      days.push(Strings.getLang(`week${index}`));
    }
  });
  if (workDay && weekend) {
    repeat = Strings.getLang('daily');
  } else if (workDay && days.length === 5) {
    return Strings.getLang('workDay');
  } else if (weekend && days.length === 2) {
    return Strings.getLang('weekend');
  } else if (days.length === 0) {
    repeat = Strings.getLang('once');
  } else {
    repeat = days.join(' ');
  }
  return repeat;
};
